package com.thkj.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thkj.entity.RmEnum;
import com.thkj.entity.RmEnumDetail;
import com.thkj.service.RmEnumService;

import dp.dao.PagingList;
import com.thkj.framework.base.BaseService;
import dp.util.MapUtil;
import dp.util.UUIDUtil;

@Service
public class RmEnumServiceImpl extends BaseService implements RmEnumService {
    public void insert(RmEnum t){
    	RmEnum rmEnum = this.getById(t.getEnumCd());
    	if(rmEnum!=null){
    		throw new IllegalArgumentException("编号重复");
    	}
        dao.insert("com.thkj.mapper.RmEnum.insert", t);
    }

    /**
     * 修改代码表，如果编号发生了变化则需要修改明细表的编号
     */
    @Transactional
    public int update(String oldEnumCd, RmEnum t){
    	RmEnum rmEnum = this.getById(t.getEnumCd());
    	
    	if(rmEnum!=null && !oldEnumCd.equals(rmEnum.getEnumCd())){
    		throw new IllegalArgumentException("编号重复");
    	}
    	
    	Map param = MapUtil.createMap("oldEnumCd", oldEnumCd, "rmEnum", t);
        int row = dao.update("com.thkj.mapper.RmEnum.update", param);
        if(!StringUtils.equals(oldEnumCd, t.getEnumCd())){
        	dao.update("com.thkj.mapper.RmEnumDetail.updateEnumCd", param);
        }
        return row;
    }

    @Transactional
    public int delete(String t ){
        dao.delete("com.thkj.mapper.RmEnumDetail.deleteByEnumCd", t);
        return dao.delete("com.thkj.mapper.RmEnum.delete", t);
    }

    public RmEnum getById(String t){
        return dao.queryForObject("com.thkj.mapper.RmEnum.getById", t, RmEnum.class);
    }

	public PagingList<RmEnum> query(int pageIndex, int pageSize, Map<String, Object> param) {
		return dao.queryForPagingList("com.thkj.mapper.RmEnum.query",param, pageIndex, pageSize, RmEnum.class);
	}

	public List<RmEnumDetail> getDetail(String enumCd, String enabled) {
		return dao.queryForList("com.thkj.mapper.RmEnumDetail.query", MapUtil.createMap("enumCd", enumCd, "enabled", enabled), RmEnumDetail.class);
	}

	public void insertDetail(RmEnumDetail detail){
		List<RmEnumDetail> list = dao.queryForList("com.thkj.mapper.RmEnumDetail.query", MapUtil.createMap("enumCd", detail.getEnumCd(), "enumDetailValue", detail.getEnumDetailValue()), RmEnumDetail.class);
		if(!list.isEmpty()){
    		throw new IllegalArgumentException("值不能重复");
    	}
		detail.setEnumDetailId(UUIDUtil.randomUUID());
		dao.insert("com.thkj.mapper.RmEnumDetail.insert", detail);
	}
	
	public int updateDetail(RmEnumDetail detail){
		List<RmEnumDetail> list = dao.queryForList("com.thkj.mapper.RmEnumDetail.query", MapUtil.createMap("enumCd", detail.getEnumCd(), "enumDetailValue", detail.getEnumDetailValue()), RmEnumDetail.class);
		if(list.isEmpty() || (list.size()==1 && StringUtils.equals(list.get(0).getEnumDetailId(), detail.getEnumDetailId()))){
			return dao.update("com.thkj.mapper.RmEnumDetail.update", detail);
		}
		else{
			throw new IllegalArgumentException("值不能重复");
		}
	}

	public int deleteDetail(String detailId) {
		return dao.delete("com.thkj.mapper.RmEnumDetail.delete", detailId);
	}

	@Override
	public List<Map> queryEnumDetail(String enumCd, String enabled) {
		return dao.queryForList("com.thkj.mapper.RmEnumDetail.queryEnumDetail", MapUtil.createMap("enumCd", enumCd, "enabled", enabled), Map.class);
	}

	@Override
	public String getEnumDetailLabel(String enumCd, String enumDetailValue) {
		Map map = new HashMap<>();
		map.put("enumCd", enumCd);
		map.put("enumDetailValue", enumDetailValue);
		return dao.queryForObject("com.thkj.mapper.RmEnum.getEnumDetailLabel", map, String.class);
	}

}